import { Component } from "cc";
import BaseData from "./BaseData";
import { EventTouch } from "cc";
import { Node } from "cc";
import ResMgr, { TProgress } from "../manager/ResMgr";
import { IBaseCtrl } from "./IBaseCtrl";
import { warn } from "cc";

export interface IViewParams {
    [key: string]: any,
    /** 界面打开回调 */
    opened?: () => void,
    /** 界面打开后，会关闭这个界面 */
    closeModuleWhenOpen?: number,
}

export default class BaseView extends Component {
    /** 模块key */
    module: number|string = -1;
    /** 模块控制层 */
    ctrl: IBaseCtrl = null;
    /** 模块数据层 */
    data: BaseData = null;
    /** 外部参数 */
    param: IViewParams = null;
    /** 节点表 */
    ui: { [uiName: string]: Node } = {};

    __preload() {
        super.__preload && super.__preload();
        this.walkUI(this.node);
    }

    walkUI(uiRoot: Node) {
        if (uiRoot.name.charAt(0) == "_") return;
        if (this.ui[uiRoot.name] != null) {
            // warn("节点树重复", uiRoot.name);
        }
        this.ui[uiRoot.name] = uiRoot;
        uiRoot.children.forEach((c) => this.walkUI(c));
    }

    regTouchEvent(node: Node, callBack: Function, target: Component | Node) {
        node.on(Node.EventType.TOUCH_END, callBack, target);
    }

    /** 界面打开回调 */
    onOpen(p: IViewParams) { }

    /** 关闭回调，由uiMgr复写 */
    onBtnClose(e: EventTouch, isDel: boolean = true) {

    }

    /** 关闭界面方法 */
    close(isDel: boolean = true) {
        this.onBtnClose(null, isDel);
    }

    setProgress(percent: number) {

    }
}

